<?php
/**
 * Created by PhpStorm.
 * User: pangaofeng
 * Date: 16/3/5
 * Time: 上午8:29
 */

//namespace Vendor\Alipay;


require_once 'lib/alipay_core.function.php';
require_once 'lib/alipay_md5.function.php';
require_once 'lib/alipay_rsa.function.php';
require_once 'lib/alipay_submit.class.php';
require_once 'lib/alipay_notify.class.php';

/**
 * Class Alipay
 * @package Vendor\Alipay
 */
class Alipay
{
    private $Config = array(
        'server'       =>  'create_direct_pay_by_user',    //接口名称默认为即时到账
        'partner'       =>  '',                             //合作身份ID
        'seller_email'  =>  '',                             //支付宝账户
        'key'           =>  '',                             //安全效验码
        'sign_type'     =>  'MD5',                          // RSA也可以
        //'input_charset'=>  'utf-8',                        //编码配置,不可修改
        '_input_charset'=>  'utf-8',                        //编码配置,不可修改
        'transport'     => 'http',                          //如果服务器支持SSL,请选用https
    );

    /**
     * Alipay constructor.
     * @param string $seller_email 支付宝账户
     * @param string $partner 合作身份ID
     * @param string $key 安全效验码
     * @param string $transport
     */
    public function __construct(){
//        $this->Config['seller_email'] = Config::alipay('seller_email');
//        $this->Config['partner']      = Config::alipay('partner');
//        $this->Config['key']          = Config::alipay('key');
//        $this->Config['ali_public_key'] = Config::alipay('ali_public_key');
//        $this->Config['private_key'] = Config::alipay('private_key');
        $this->Config['seller_email'] = ' jzbwlkjtest@163.com';
        $this->Config['partner']      = '2088421701715110';
        $this->Config['key'] = 'nru9wktdvg2obedfzlgpa2ws2ucddnbf';
//        $this->Config['ali_public_key'] = file_get_contents('./ali_public_key.pem');
//        $this->Config['private_key'] = file_get_contents('./private_key.pem');
        $this->Config['ali_public_key'] = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB';
        $this->Config['ali_public_key'] = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB';
        $this->Config['private_key'] = 'MIICWwIBAAKBgQDa4HPf5kITzHPBe57781pyHJw1wdPlHzjqXtUEmCDTdxmOEBX0AaTzPmEM9WvI6HZniWUV8045R7ssvpIYw4WhLnpGUDeHIAiPeEWHH/DuzlnRXvvoioD9IcuONRuo60LAadP8ILw3RHXVDYhogmjohwtpFfPXuIUg0cZ9TaD3bwIDAQABAoGAVm68U3aCMX9TVtySc7wtw1I4/aGFI72opCS+ygmpLNijZs5DXRApK0U+3fcskTH7T/eZy4lBIq2I5Kp81vqfEcRslcrcJtcAjTyv+UxwXhRgcayIqxeLONdbwWXaB5PvtDtU/MMimqbtQy+QjjNpxckzgTvFNRQGizT8WAUP1wECQQDtKzt9kJw44B7PVCsco7X8Bzz0pDNtCbvjVBd96z7yizbHpgdm24oRVt7SeaRjL/+vOOwDhaQN6Zu2XSYPjbsxAkEA7EFows5PSxxepQOASr5b/U6kbjJwWFvKmI4q987d2DMk3YSvuDjar9Z0Kj6BnBjE4veoXZ+nCERSRJDbx1/0nwJADeOgQkQ7weDuLPoi5DR6Ld5GpZUQJeySAkgkcA1czFt2w85nVJC6zzUlK3JQPDbNZHeS6snix6XMpqth1TtZoQJASnWm/dal+Qc88Qb0dRofxer2OeDdtYtgg8oy0p05H/2mfcZm10nb6pVNUU8X7EmGK/5f6vn5W8qknwWErwaYAwJAH7RR30Ln8AyceV8jUrnfhHahUhanoco9rda9BiZHfI7I8m5vR485TbELEuoGmXjSGFkq/vNgcfNq4nP2H/d2Ug==';



    }

    /**
     * @param string $subject 商品名 不知为何如果是中文就乱码
     * @param string $total_fee 价格   要支付的价格
     * @param string $out_trade_no 订单号
     * @param string $notify_url 通知网址
     * @param string $return_url 回调网址
     * @param string $btn_value
     * @return 提交表单HTML文本
     */
    public function buildPayForm($subject,$total_fee,$out_trade_no,$notify_url,$return_url,$btn_value='确认支付'){
        $parameter = array(
            'service'       =>  $this->Config['server'],
            'partner'       =>  trim($this->Config['partner']),
            'seller_email'  => $this->Config['seller_email'],
            'payment_type'  => 1,
            'out_trade_no'  => $out_trade_no,
            'notify_url'    => $notify_url,
            'subject'       => $subject,
            'total_fee'     => $total_fee,
            'return_url'    => $return_url,
            '_input_charset' => $this->Config['_input_charset'],
        );
        $alipaySubmit = new AlipaySubmit($this->Config);
        return $alipaySubmit->buildRequestForm($parameter,"get", $btn_value);
    }

    /**
     * 返回验证 返回[订单号=>trade_no, 金额=>total_fee]或者False
     */
    public function verifyReturn(){
        $alipayNotify = new AlipayNotify($this->Config);
        return $alipayNotify->verifyReturn();
    }
    /**
     * 通知验证 返回[订单号=>trade_no, 金额=>total_fee]或者False
     */
    public function verifyNotify(){
        $alipayNotify = new AlipayNotify($this->Config);
        return $alipayNotify->verifyNotify();
    }
}